package com.android.plc.ui.control;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewGroup;

import com.android.plc.CONST;
import com.android.plc.ui.ControlManager;
import com.android.plc.ui.view.BaseControlView;

public abstract class BaseControl
{
	public static final String TAG = "BaseControl";

	public Context mContext;
	public ViewGroup mCtrolView;
	protected ControlManager mControlManager;
	public static int OFF_ALPHA = 128;
	public static int ON_ALPHA = 255;

	public BaseControl(Context context, ViewGroup view, int res_id)
	{
		mContext = context;
		mCtrolView = new BaseControlView(mContext, res_id);
	};

	public abstract void initView(ViewGroup view);

	public abstract void onReceive(int command, int data1, int data2);

	public void setView(ViewGroup view)
	{
		mCtrolView = view;
	}

	public ViewGroup getView()
	{
		return mCtrolView;
	}

	public void setControlManager(ControlManager mControlManager)
	{
		this.mControlManager = mControlManager;
	}

	public ControlManager getControlManager()
	{
		return mControlManager;
	}

	public void sendCommand(int comand, int data1, int date2)
	{
		sendCommand(0, comand, data1, date2);
	}

	public void sendCommandDelay(int comand, int data1, int date2)
	{
		Message msg = Message.obtain();
		msg.what = comand;
		msg.arg1 = data1;
		msg.arg2 = date2;
		Log.d(TAG, "sendMessageDelayed");
		mHandler.removeMessages(comand);
		mHandler.sendMessageDelayed(msg, CONST.SLEEP_TIME);
	}

	private final int UNIT_MSG = 0x1000;
	private Handler mHandler = new Handler()
	{

		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			Log.d(TAG, "handleMessage");
			if (msg.what == UNIT_MSG)
			{
				Bundle data = msg.getData();
				if (data != null)
				{
					int home_unit = data.getInt("home_unit");
					int comand = data.getInt("comand");
					int data1 = data.getInt("data1");
					int date2 = data.getInt("date2");
					mControlManager.sendUnitCommand(home_unit, comand, data1, date2);
					return;
				}
			}
			mControlManager.sendControlCommand(0, msg.what, msg.arg1, msg.arg2);
		}

	};

	public void sendCommand(int offset, int comand, int data1, int date2)
	{
		mControlManager.sendControlCommand(offset, comand, data1, date2);
	}

	public void sendUnitCommand(int home_unit, int comand, int data1, int date2, long sleep_time)
	{
		Message msg = Message.obtain();
		msg.what = UNIT_MSG;
		Bundle data = new Bundle();
		data.putInt("home_unit", home_unit);
		data.putInt("comand", comand);
		data.putInt("data1", data1);
		data.putInt("date2", date2);
		msg.setData(data);
		mHandler.sendMessageDelayed(msg, sleep_time);
	}

	public void attachToControlManager(ViewGroup view)
	{
		view.addView(mCtrolView);
		initView(view);
	}

	public void detachFromControlManager(ViewGroup view)
	{
		view.removeView(mCtrolView);
	}
}
